package com.itzz.spring_zhoneHeng.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.BookDao;
import com.itzz.spring_zhoneHeng.dao.BookShelfDao;
import com.itzz.spring_zhoneHeng.dao.UserDao;
import com.itzz.spring_zhoneHeng.pojo.Book;
import com.itzz.spring_zhoneHeng.pojo.BookShelf;
import com.itzz.spring_zhoneHeng.service.BookShelfService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BookShelfServiceImpl implements BookShelfService {

    @Autowired
    BookShelfDao bookShelfDao;

    @Autowired
    BookDao bookDao;

    @Autowired
    UserDao userDao;

    //查询所有的添加书架的书籍
    @Override
    public RestResponse queryAllBookShelf(BookShelf bookShelf, Integer pageNum, Integer pageSize,Integer userid) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //根据用户名查询到 书架的id
        List<Integer> bsids2 = userDao.queryByBsid(userid);
        //根据ids查询书籍
        Integer[] integers = bsids2.toArray(new Integer[bsids2.size()]);
        List<BookShelf> bookShelves = bookShelfDao.queryAllBookShelf
                (bookShelf.getBookName(),bookShelf.getAuthor(),
                bookShelf.getCategory(),bookShelf.getVip(),
                integers);
        //添加到分页插件中
        PageInfo<BookShelf> chaptersPageInfo = new PageInfo<>(bookShelves);
        if(bookShelf!=null){
            return RestResponse.ok(chaptersPageInfo);
        }
            return RestResponse.fail(0,"不存在书籍");
    }


    //添加书架的书籍
    @Override
    @Transactional
    public RestResponse addBook(BookShelf bookShelf, Integer id, Integer intestate,Integer userId) {
        if (id==null) {
            return RestResponse.fail(400,"书籍不存在了");
        }
        if(id!=null&&id>0){
            intestate=2;
        }
        //添加书架的书籍        添加dao表bookshelf
        if(bookShelf!=null){
            bookShelf.setId(0);
        }
        //首先先添加表bookshelf
        bookShelfDao.addBookShelf(bookShelf);
        //添加书架和书籍的关联表   b_bs表
        bookShelfDao.addBookShelfAndBook(id,bookShelf.getId());
        //添加用户和书架的关系表   u_bs表
        int i = bookShelfDao.addUserAndBookShelf2(userId, bookShelf.getId());


        if (i>0){
            return RestResponse.ok(i);
        }else {
            return RestResponse.fail(400,"添加失败");
        }
    }



    //删除书架的书籍
    @Override
    @Transactional
    public RestResponse deleteBook(Integer id) {

        //修改书籍的状态
//        if(id!=null&&id>0){
//            bookshelf=0;
//        }
        //根据书架的id查询书籍的id
        Integer bookid = bookDao.queryBookId(id);
//        //根据书籍的id修改书架的状态
//        bookShelfDao.updateBookShelf(bookid,bookshelf);
        //根据id删除书架书籍   bookshelf表
        bookShelfDao.deleteBookShelf(id);
        //根据id删除书架和书籍的关联表
        bookShelfDao.deleteBookShelfAndBook(id);
        //根据bsid删除用户和书架的关系表(u_bs)b_bs
        int i = bookShelfDao.delU_BS(id);


        if (i>0){
            return RestResponse.ok(i);
        }
        return RestResponse.fail(0,"删除失败");
    }

    //判断我前台点击的书是否存在于我的书架中
    @Override
    @Transactional
    public RestResponse queryBookShelf(Integer userId, Integer bookId){
        //根据用户id 查询表u_bs中的bsid
        List<Integer> bookshelfId = bookShelfDao.queryBsid(userId);
        //判断bookshelfId是否为空
        if(bookshelfId!=null&&bookshelfId.size()>0){
            //根据bsid 查询b_bs  中的bookid
            Integer[] bsids = bookshelfId.toArray(new Integer[bookshelfId.size()]);
            List<Integer> bookids = bookShelfDao.queryBookId(bsids);
            for (Integer bookid:bookids) {
                if(bookid.equals(bookId)){
//                说明已经添加到书架中了
                    return RestResponse.ok();
                }
            }
        }
        return RestResponse.fail(0,"书籍不存在书架中");

    }





    //判断书籍的书是否存在于订单中
    @Override
    @Transactional
    public RestResponse queryBookId(Integer bookid){
        //根据bookid 查询b_bs  中的bsid
        Integer bsid = bookShelfDao.queryBookShelfId(bookid);
        //根据bsid   查询bookshelf(id)  中的对应的书籍是否存在
        BookShelf bookShelf = bookShelfDao.queryBookShelfById(bsid);
        //如果bookshelf存在  说明已经添加到订单中了
        //否则  说明没有添加到订单中
        if (bookShelf==null){
            return RestResponse.fail(400,"订单不存在");
        }
        return RestResponse.ok();
    }

    @Override
    public RestResponse queryBookShelfById(Integer bookshelfid) {
        BookShelf bookShelf = bookShelfDao.queryBookShelfById2(bookshelfid);
        if(bookShelf!=null){
            return  RestResponse.ok(bookShelf);
        }
        return RestResponse.fail(0,"书籍不存在");
    }

    //根据bookshelfId 查询表 b_bs表的 bookid
    //queryBookIdByBsid
    @Override
    public RestResponse queryBookIdByBsid(Integer bsid) {
        Integer bookid = bookShelfDao.queryBookIdByBsid(bsid);

        return  RestResponse.ok(bookid);
    }


}
